package com.huike.controller.adminController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huike.entity.Admin;
import com.huike.entity.Information;
import com.huike.service.IInformationService;
import com.huike.utils.JsonUtils;
import com.huike.utils.JsonUtilsForRichText;
import com.huike.utils.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 联盟资讯管理控制器
 */
@Controller
@RequestMapping("/admin/unionInformationManagerController")
public class UnionInformationManagerController {

    @Autowired
    IInformationService service;

    /**
     * 解决页面传来时间格式转换失败问题
     * @param binder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 资讯管理页面
     * @return
     */
    @RequestMapping("")
    public String page(){
        return "admin/unionInformationManager";
    }

    /**
     * 分页显示资讯
     * @param response
     * @param page
     * @param limit
     */
    @RequestMapping("/listInfomation")
    @ResponseBody
    public void listInfomation(HttpServletResponse response, HttpServletRequest request,int page, int limit){
        HttpSession session=request.getSession();
        Admin admin=(Admin)session.getAttribute("admin");
        IPage<Information> res = service.listByPage(admin,page, limit);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("total",res.getTotal());
        map.put("data",res.getRecords());
        JsonUtilsForRichText.jsonArray(map,response);
    }

    /**
     * 添加方法
     * @param request
     * @param information
     * @return
     */
    @RequestMapping("/addInfomation")
    @ResponseBody
    public ResultMessage addInfomation(HttpServletRequest request,Information information){
        HttpSession session = request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        boolean b = service.insertInfomation(admin, information);
        if (b)
            return ResultMessage.success();
        return ResultMessage.fail();
    }

    /**
     * 修改方法
     * @param information
     * @return
     */
    @RequestMapping("/updateFunc")
    @ResponseBody
    public ResultMessage updateFunc(Information information){
        boolean b = service.updateById(information);
        if (b)
            return ResultMessage.success();
        return ResultMessage.fail();
    }

    /**
     * 删除方法
     * @param ids
     * @return
     */
    @RequestMapping("/deleteInformation")
    @ResponseBody
    public ResultMessage deleteInformation(@RequestParam(required = false,value = "ids[]")List<String> ids){
        boolean b = service.deleteInformation(ids);
        if (b)
            return ResultMessage.success();
        return ResultMessage.fail();
    }
}
